package grape.composite.rest.subway.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.composite.rest.subway.form.SubwayListForm;
import grape.composite.service.subway.query.SubwayListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.composite.rest.subway.form.SubwayCreateForm;
import grape.composite.rest.subway.form.SubwayUpdateForm;
import grape.composite.rest.subway.form.SubwayListPageForm;
import grape.composite.rest.subway.vo.SubwayVo;
import grape.composite.rest.subway.mapper.SubwayWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.composite.service.subway.po.Subway;
import grape.composite.service.subway.api.ISubwayService;
import java.util.List;
/**
 * <p>
 * 地铁线路 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-10
 */
@RestController
@RequestMapping("/composite/subway")
@Api(tags = "地铁线路相关接口")
public class SubwayController extends BaseController<SubwayVo, Subway> {

    @Autowired
    private SubwayWebMapper currentWebMapper;
    @Autowired
    private ISubwayService currentService;



     @ApiOperation("添加地铁线路")
     @PreAuthorize("hasAuthority('subway:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public SubwayVo create(@RequestBody @Valid SubwayCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         Subway po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询地铁线路")
     @PreAuthorize("hasAuthority('subway:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public SubwayVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除地铁线路")
     @PreAuthorize("hasAuthority('subway:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新地铁线路")
     @PreAuthorize("hasAuthority('subway:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public SubwayVo update(@PathVariable String id,@RequestBody @Valid SubwayUpdateForm uf) {
         Subway po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询地铁线路")
    @PreAuthorize("hasAuthority('subway:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<SubwayVo> list(SubwayListForm listForm) {
        Subway po = currentWebMapper.formToPo(listForm);
        SubwayListQuery subwayListQuery = new SubwayListQuery();
        subwayListQuery.setPoQuery(po);
        return super.list(subwayListQuery);
    }

    @ApiOperation("分页查询地铁线路")
    @PreAuthorize("hasAuthority('subway:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<SubwayVo> listPage(SubwayListPageForm listPageForm) {
         Subway po = currentWebMapper.formToPo(listPageForm);
        SubwayListQuery subwayListQuery = new SubwayListQuery();
        subwayListQuery.setPoQuery(po);
         return super.listPage(subwayListQuery,listPageForm);
     }

}
